<template>
  <el-dialog v-model="visible" title="比赛报名" width="500" :before-close="handleClose" destroy-on-close>
    <el-form ref="formRef" :model="form">
<!--      <el-form-item label="Resources">-->
<!--        <el-radio-group v-model="form.resource">-->
<!--          <el-radio value="Sponsor">Sponsor</el-radio>-->
<!--          <el-radio value="Venue">Venue</el-radio>-->
<!--        </el-radio-group>-->
<!--      </el-form-item>-->
      <el-form-item label="男单打" :label-width="formLabelWidth" prop="nanOne">
        <el-radio-group v-model="form.nanOne" :disabled="userStore.userInfo.sex!='男'">
          <el-radio label="1">报名</el-radio>
          <el-radio label="2">不报名</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="男双打" :label-width="formLabelWidth" prop="nanTwo">
        <el-radio-group v-model="form.nanTwo" :disabled="userStore.userInfo.sex!='男'">
          <el-radio label="1">报名</el-radio>
          <el-radio label="2">不报名</el-radio>
        </el-radio-group>
<!--        <span style="margin-left: 10px;"><el-link disabled :underline="false" type="success">复制男双打邀请连接</el-link></span>-->
      </el-form-item>
      <el-form-item label="女单打" :label-width="formLabelWidth" prop="nvOne">
        <el-radio-group v-model="form.nvOne" :disabled="userStore.userInfo.sex!='女'">
          <el-radio label="1">报名</el-radio>
          <el-radio label="2">不报名</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="女双打" :label-width="formLabelWidth" prop="nvTwo">
        <el-radio-group v-model="form.nvTwo" :disabled="userStore.userInfo.sex!='女'">
          <el-radio label="1">报名</el-radio>
          <el-radio label="2">不报名</el-radio>
        </el-radio-group>
<!--        <span style="margin-left: 10px;"><el-link :underline="false" type="success">复制女双打邀请连接</el-link></span>-->
      </el-form-item>

      <el-form-item label="男女混打" :label-width="formLabelWidth" prop="allTwo">
        <el-radio-group v-model="form.allTwo">
          <el-radio label="1">报名</el-radio>
          <el-radio label="2">不报名</el-radio>
        </el-radio-group>
<!--        <span style="margin-left: 10px;"><el-link :underline="false" type="success">复制男女混打邀请连接</el-link></span>-->
      </el-form-item>
      <el-alert type="info" show-icon :closable="false">
        <p>选中报名类型后，点击提交，对于双人报名的邀请连接可以在【我的报名】里复制邀请连接发送给队员，并且可以查看报名状态是否成功，如果在报名截止时间没有邀请到队员责取消资格或者匹配其他没有邀请到队员的人。
        </p>
      </el-alert>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit">
          报名
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {FormInstance} from "element-plus";
import { postCompetitionApply } from '~~/api/competition'
import {useUserStore} from "~/stores/user";
const emit = defineEmits(['success', 'close'])
const userStore = useUserStore()
const visible = ref(false)
const formLabelWidth = '140px'
const formRef = shallowRef<FormInstance>();
const props = defineProps({
  competitionId: {
    type: Number,
    required: true
  },
  data: {
    type: Array as PropType<any[]>,
    default: () => []
  }
});
const form = reactive({
  nanOne: '',
  nanTwo: '',
  nvOne: '',
  nvTwo: '',
  allTwo: '',
  competitionId: ''
})
const init = () => {
  Object.assign(form, props.data);
  // Object.keys(form).forEach((key) => {
  //   //@ts-ignore
  //   form[key] = props.data[key]
  // })
  // console.log(props.data)
  // console.log(form)
}
const handleSubmit = async () => {
  await formRef.value?.validate()
  // mode.value == 'edit' ? await articleCateEdit(formData) : await articleCateAdd(formData)
  form.competitionId = props.competitionId
  await postCompetitionApply(form)
  visible.value = false
  emit('success')
}

const handleClose = () => {
  emit('close')
}

const open = () => {
  visible.value = true
}
console.log(props.data)
init()
defineExpose({
  open
})
</script>

<style scoped>

</style>